<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
	//登陆页面
    public function index()
    {
        $this->display();
    }

    //登陆
    public function login()
    {
        if (IS_POST) {
            $username = I('post.username');
            $password = I('post.password');

            $map['username'] = $username;
            $map['password'] = md5($password);
            $res = M('user')->where($map)->find();
            if ($res) {
                $url = U('admin/index/index');
                $arr = array(
                    'error' => 1 ,
                    'info' => '登陆成功',
                    'url' => $url
                    );
                echo json_encode($arr);

                //写入session
                //更新登陆时间 ip
                $login_time= time();
                $ip = get_client_ip();
                $data = array('login_time'=>$login_time,'ip'=>$ip);
                M('user')->where(array('id'=>$res["id"]))->setField($data);

                //设置session登录成功
                $_SESSION["id"] = $res["id"];
                $_SESSION["username"] = $res["username"];

            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '账号密码错误'
                    );
                echo json_encode($arr);
            }
        }
    }


    //退出
    public function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('admin/user/index');
    }


    //用户列表
    public function userlist()
    {
        $users = M('user')->select();
        $this->assign('users',$users);

        $this->display();
    }

    //处理状态 设为处理1
    public function is_dj()
    {
        $id = I('post.id');
        $res = M('user')->where(array('id'=>$id))->setField('is_dj',1);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '账户冻结成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '账户冻结失败'
                );
            echo json_encode($arr);
        }
    }
    //处理状态 设为处理0 解冻
    public function is_dj_no()
    {
        $id = I('post.id');
        $res = M('user')->where(array('id'=>$id))->setField('is_dj',0);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '账户解冻成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '账户解冻失败'
                );
            echo json_encode($arr);
        }
    }
    //删除
    public function delete()
    {
        $id = I('post.id');
        $res = M('user')->delete($id);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '账户删除成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '账户删除失败'
                );
            echo json_encode($arr);
        }
    }


    //浏览记录显示
    public function history()
    {
        $his = M('admin_history'); // 实例化User对象
        $count      = $his->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $sql = 'select * from admin_history limit '. $Page->firstRow.','.$Page->listRows;
        $his = M()->query($sql);
        $this->assign('his',$his);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }


}